package cn.kailangtu.dataspot.data.context;

import android.graphics.Color;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;

import cn.kailangtu.commonevents.eventEntity.KeyboardNumberCloseEvent;
import cn.kailangtu.commontools.ToastUtils;
import cn.kailangtu.dataspot.DataSpotApplication;

public class KeyboardContext {
    public static String result = "";
    public static boolean isFirstEdit = true;
    private  static TextView selectedView;
    private static int normalColor;
    public static void setSelectedView(TextView newView)
    {

        if(selectedView != null)
        {
            selectedView.setTextColor(normalColor);
        }
        normalColor = newView.getCurrentTextColor();
        selectedView = newView;
        newView.setTextColor(Color.parseColor("#FF0000"));

    }

    public static TextView getSelectedView()
    {
        return selectedView;
    }

    public static boolean addValue(String newValue)
    {
       if(isFirstEdit)
       {
           isFirstEdit = false;
           result=newValue;
           return true;
       }
        if(result.length() <= 16)
        {
            if(newValue.equals("."))
            {
                if(!result.endsWith("."))
                {
                    result += newValue;
                    return true;
                }
            }
            else {
                result += newValue;
                return true;
            }
        }
        return false;
    }

    public static boolean delValue()
    {
        if(result.length()> 0)
        {
            result = result.substring(0,result.length()-1);
            return true;
        }
        return false;
    }

    public static void entry()
    {
        if(selectedView != null)
        {
            switch(selectedView.getTag().toString()) {
                case "100":
                    if(validate()) {
                        EventBus.getDefault().post(new KeyboardNumberCloseEvent(result, false));
                    }
                    break;
                case "-1":
                    delValue();
                    break;
                case "-100":

                    EventBus.getDefault().post(new KeyboardNumberCloseEvent(result,true));
                    break;
                default:
                    addValue(selectedView.getText().toString());
                    break;
            }
        }
    }

    private static boolean validate()
    {
        String msg = "数据不是有效数字";
        boolean result = false;
        float minValue = Float.MIN_VALUE;
        float maxValue = Float.MAX_VALUE;
        if(KeyboardTargetContext.controlTreeNode != null  ) {
            if (KeyboardTargetContext.controlTreeNode.isNorule()) {
                //如果配置了无规则，就不校验了，比如192.168.100.1这种IP
                return true;
            } else {
                minValue = KeyboardTargetContext.controlTreeNode.getMinValue();
                maxValue = KeyboardTargetContext.controlTreeNode.getMaxValue();
            }
        }
        try {
            Float currentValue = Float.parseFloat(KeyboardContext.result);
            if (currentValue < minValue || currentValue > maxValue) {
                msg = "值范围[" + minValue + "到" + maxValue + "]";
            } else {
                result = true;
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }

        if(!result) {
            //EventBus.getDefault().post(new NewMessageEvent(msg));
            ToastUtils.ShowInCenter(DataSpotApplication.getContext(),msg);
        }
        return result;
    }
}
